//
//  AssistantViewController.m
//  云律司
//
//  Created by 韦淋 on 2017/9/15.
//  Copyright © 2017年 test. All rights reserved.
//

#import "AssistantViewController.h"



static NSString *reuseID = @"itemCell";
static NSString *sectionHeaderID = @"sectionHeader";

@interface AssistantViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
{
    NSIndexPath *_originalIndexPath;
    NSIndexPath *_moveIndexPath;
    UIView *_snapshotView;
}
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *itemGroups;
@property (nonatomic, strong) NSArray *allItemModel;

@end

@implementation AssistantViewController



- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    //    self.automaticallyAdjustsScrollViewInsets = false;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.barTitleLable.text = @"律师助手 ";
    self.view.backgroundColor = [UIColor colorWithRed:238.0f/255.0f green:238.0f/255.0f blue:238.0f/255.0f alpha:1.0f];
    [self setupCollectionView];
}



- (void)setupCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(SCREEN_WIDTH / 4, SCREEN_WIDTH / 4);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 35);
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 64, self.view.bounds.size.width, self.view.bounds.size.height - 64) collectionViewLayout:layout];
    _collectionView.backgroundColor = [UIColor whiteColor];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[ItemCell class] forCellWithReuseIdentifier:reuseID];
    [_collectionView registerClass:[ItemCellHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionHeaderID];
    
}


#pragma mark - <UICollectionViewDelegate, UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.itemGroups.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    ItemGroup *group = self.itemGroups[section];
    return group.items.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
    //    cell.delegate = self;
    //    cell.backgroundColor = kRandomColor;
    ItemGroup *group = self.itemGroups[indexPath.section];
    ItemModel *itemModel = group.items[indexPath.row];
    if (indexPath.section != 0) {
        BOOL isAdded = NO;
        ItemGroup *homeGroup = self.itemGroups[0];
        for (ItemModel *homeItemModel in homeGroup.items) {
            
            if ([homeItemModel.itemTitle isEqualToString:itemModel.itemTitle]) {
                isAdded = YES;
                break;
            }
        }
        
        if (isAdded) {
            itemModel.status = StatusCheck;
        }else {
            itemModel.status = StatusPlusSign;
        }
    }
    cell.itemModel = group.items[indexPath.row];
    cell.indexPath = indexPath;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
    if (kind == UICollectionElementKindSectionHeader) {
        ItemCellHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:sectionHeaderID forIndexPath:indexPath];
        
        ItemGroup *group = self.itemGroups[indexPath.section];
        headerView.title = group.type;
        
        return headerView;
    }else {
        return nil;
    }
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return UIEdgeInsetsMake(0, 0, 10, 0);
    }else {
        return UIEdgeInsetsMake(0, 0, 1 / [UIScreen mainScreen].scale, 0);
    }
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    AssistantDetailController *ADVC = [[AssistantDetailController alloc] init];
    if (indexPath.section == 0) {
        NSLog(@"第一组 ");
        if (indexPath.row == 0) {
            NSLog(@"宪法 ");
            ADVC.lawType = @"1";
        }
        if (indexPath.row == 1) {
            NSLog(@"宪法相关法 ");
            ADVC.lawType = @"2";
        }
        if (indexPath.row == 2) {
            NSLog(@"民法商法 ");
            ADVC.lawType = @"3";
        }
        if (indexPath.row == 3) {
            NSLog(@"行政法 ");
            ADVC.lawType = @"4";
        }
        if (indexPath.row == 4) {
            NSLog(@"经济法 ");
            ADVC.lawType = @"5";
        }
        if (indexPath.row == 5) {
            NSLog(@"社会法 ");
            ADVC.lawType = @"6";
        }
        if (indexPath.row == 6) {
            NSLog(@"刑法 ");
            ADVC.lawType = @"7";
        }
        if (indexPath.row == 7) {
            NSLog(@"诉讼及非诉讼程序法 ");
            ADVC.lawType = @"8";
        }
    }
    if (indexPath.section == 1) {
        NSLog(@"第二组 ");
        if (indexPath.row == 0) {
            NSLog(@"行政法规及文件 ");
            ADVC.lawType = @"9";
        }
        if (indexPath.row == 1) {
            NSLog(@"部委规章及文件 ");
            ADVC.lawType = @"10";
        }
        if (indexPath.row == 2) {
            NSLog(@"法院 ");
            ADVC.lawType = @"11";
        }
        if (indexPath.row == 3) {
            NSLog(@"检察院 ");
            ADVC.lawType = @"12";
        }
    }
    if (indexPath.section == 2) {
        NSLog(@"第三组 ");
        if (indexPath.row == 0) {
            NSLog(@"北京 ");
            ADVC.lawType = @"13";
        }
        if (indexPath.row == 1) {
            NSLog(@"天津 ");
            ADVC.lawType = @"14";
        }
        if (indexPath.row == 2) {
            NSLog(@"河北 ");
            ADVC.lawType = @"15";
        }
        if (indexPath.row == 3) {
            NSLog(@"山西 ");
            ADVC.lawType = @"16";
        }
        if (indexPath.row == 4) {
            NSLog(@"内蒙古 ");
            ADVC.lawType = @"17";
        }
        if (indexPath.row == 5) {
            NSLog(@"辽宁 ");
            ADVC.lawType = @"18";
        }
        if (indexPath.row == 6) {
            NSLog(@"吉林 ");
            ADVC.lawType = @"19";
        }
        if (indexPath.row == 7) {
            NSLog(@"黑龙江 ");
            ADVC.lawType = @"20";
        }
        if (indexPath.row == 8) {
            NSLog(@"上海 ");
            ADVC.lawType = @"21";
        }
        if (indexPath.row == 9) {
            NSLog(@"江苏 ");
            ADVC.lawType = @"22";
        }
        if (indexPath.row == 10) {
            NSLog(@"浙江 ");
            ADVC.lawType = @"23";
        }
        if (indexPath.row == 11) {
            NSLog(@"安徽 ");
            ADVC.lawType = @"24";
        }
        if (indexPath.row == 12) {
            NSLog(@"福建 ");
            ADVC.lawType = @"25";
        }
        if (indexPath.row == 13) {
            NSLog(@"江西 ");
            ADVC.lawType = @"26";
        }
        if (indexPath.row == 14) {
            NSLog(@"山东 ");
            ADVC.lawType = @"27";
        }
        if (indexPath.row == 15) {
            NSLog(@"河南 ");
            ADVC.lawType = @"28";
        }
        if (indexPath.row == 16) {
            NSLog(@"湖北 ");
            ADVC.lawType = @"29";
        }
        if (indexPath.row == 17) {
            NSLog(@"湖南 ");
            ADVC.lawType = @"30";
        }
        if (indexPath.row == 18) {
            NSLog(@"广东 ");
            ADVC.lawType = @"31";
        }
        if (indexPath.row == 19) {
            NSLog(@"广西 ");
            ADVC.lawType = @"32";
        }
        if (indexPath.row == 20) {
            NSLog(@"海南 ");
            ADVC.lawType = @"33";
        }
        if (indexPath.row == 21) {
            NSLog(@"重庆 ");
            ADVC.lawType = @"34";
        }
        if (indexPath.row == 22) {
            NSLog(@"四川 ");
            ADVC.lawType = @"35";
        }
        if (indexPath.row == 23) {
            NSLog(@"贵州 ");
            ADVC.lawType = @"36";
        }
        if (indexPath.row == 24) {
            NSLog(@"云南 ");
            ADVC.lawType = @"37";
        }
        if (indexPath.row == 25) {
            NSLog(@"西藏 ");
            ADVC.lawType = @"38";
        }
        if (indexPath.row == 26) {
            NSLog(@"陕西 ");
            ADVC.lawType = @"39";
        }
        if (indexPath.row == 27) {
            NSLog(@"甘肃 ");
            ADVC.lawType = @"40";
        }
        if (indexPath.row == 28) {
            NSLog(@"青海 ");
            ADVC.lawType = @"41";
        }
        if (indexPath.row == 29) {
            NSLog(@"宁夏 ");
            ADVC.lawType = @"42";
        }
    }
    [self.navigationController pushViewController:ADVC animated:YES];
    
}

#pragma mark -- Getter/Setter


- (void)setTitle:(NSString *)title{
    [super setTitle:title];
    self.barTitleLable.text = title;
}
- (NSArray *)itemGroups {
    if (!_itemGroups) {
        NSArray *datas = @[
                           @{@"type" : @"宪法法律 ",@"items" : @[@{@"imageName" : @"xf",@"itemTitle" : @"宪法"},@{@"imageName" : @"xxxgf",@"itemTitle" : @"宪法相关法"},@{@"imageName" : @"mfsf",@"itemTitle" : @"民法商法"},@{@"imageName" : @"xzf",@"itemTitle" : @"行政法"},@{@"imageName" : @"jjf",@"itemTitle" : @"经济法"},@{@"imageName" : @"shf",@"itemTitle" : @"社会法"},@{@"imageName" : @"xingfa",@"itemTitle" : @"刑法"},@{@"imageName" : @"ss",@"itemTitle" : @"诉讼及非诉讼程序法"}]},
                           @{@"type" : @"行政法规与司法解释",@"items" : @[@{@"imageName" : @"xz",@"itemTitle" : @"行政法规及文件"},@{@"imageName" : @"gz",@"itemTitle" : @"部委规章及文件"},@{@"imageName" : @"fy",@"itemTitle" : @"法院"},@{@"imageName" : @"jcy",@"itemTitle" : @"检察院"}]},
                           @{@"type" : @"地方性法规规章",@"items" : @[@{@"imageName" : @"cs_beijing",@"itemTitle" : @"北京"},@{@"imageName" : @"cs_tianjin",@"itemTitle" : @"天津"},@{@"imageName" : @"cs_hebei",@"itemTitle" : @"河北"},@{@"imageName" : @"cs_shanxi",@"itemTitle" : @"山西"},@{@"imageName" : @"cs_neimeng",@"itemTitle" : @"内蒙古"},@{@"imageName" : @"cs_liaoning",@"itemTitle" : @"辽宁"},@{@"imageName" : @"cs_jilin",@"itemTitle" : @"吉林"},@{@"imageName" : @"cs_heilong",@"itemTitle" : @"黑龙江"},@{@"imageName" : @"cs_shanghai",@"itemTitle" : @"上海"},@{@"imageName" : @"cs_jiangsu",@"itemTitle" : @"江苏"},@{@"imageName" : @"cs_zhejiang",@"itemTitle" : @"浙江"},@{@"imageName" : @"cs_anhui",@"itemTitle" : @"安徽"},@{@"imageName" : @"cs_fujian",@"itemTitle" : @"福建"},@{@"imageName" : @"cs_jiangxi",@"itemTitle" : @"江西"},@{@"imageName" : @"cs_shandong",@"itemTitle" : @"山东"},@{@"imageName" : @"cs_henan",@"itemTitle" : @"河南"},@{@"imageName" : @"cs_hubei",@"itemTitle" : @"湖北"},@{@"imageName" : @"cs_hunan",@"itemTitle" : @"湖南"},@{@"imageName" : @"cs_guangdong",@"itemTitle" : @"广东"},@{@"imageName" : @"cs_guangxi",@"itemTitle" : @"广西"},@{@"imageName" : @"cs_hainan",@"itemTitle" : @"海南"},@{@"imageName" : @"cs_chongqing",@"itemTitle" : @"重庆"},@{@"imageName" : @"cs_sichuan",@"itemTitle" : @"四川"},@{@"imageName" : @"cs_guizhou",@"itemTitle" : @"贵州"},@{@"imageName" : @"cs_yunnan",@"itemTitle" : @"云南"},@{@"imageName" : @"cs_xizang",@"itemTitle" : @"西藏"},@{@"imageName" : @"cs_sx",@"itemTitle" : @"陕西"},@{@"imageName" : @"cs_gansu",@"itemTitle" : @"甘肃"},@{@"imageName" : @"cs_qinghai",@"itemTitle" : @"青海"},@{@"imageName" : @"cs_ningxia",@"itemTitle" : @"宁夏"}]}];
        
        
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:datas.count];
        NSMutableArray *allItemModels = [[NSMutableArray alloc] init];
        for (NSDictionary *dict in datas) {
            ItemGroup *group = [[ItemGroup alloc] initWithDict:dict];
            if ([group.type isEqualToString:@"首页应用"]) {
                for (ItemModel *model in group.items) {
                    model.status = StatusMinusSign;
                }
            }else {
                [allItemModels addObjectsFromArray:group.items];
            }
            [array addObject:group];
        }
        _itemGroups = [array copy];
        _allItemModel = [allItemModels copy];
    }
    
    return _itemGroups;
}
@end
